﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Drawing;
using System.IO;

public partial class img : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    bool ThumbnailCallback()
    {
        return false;
    } 


    protected void Button1_Click(object sender, EventArgs e)
    {
        this.FileUpload1.SaveAs("c:\\aa.jpg");

        //製作正方形縮圖
        System.Drawing.Image.GetThumbnailImageAbort myCallback = new System.Drawing.Image.GetThumbnailImageAbort(ThumbnailCallback);
        Bitmap myBitmap = new Bitmap("c:\\aa.jpg");
        System.Drawing.Image myThumbnail = null;



        if (myBitmap.Width >= myBitmap.Height)
        {
            int w = (int)((75.0 / (float)myBitmap.Height) * (float)myBitmap.Width);

            if (w > 10000)
                w = 10000;

            int left = (w - 75) / 2;

            myThumbnail = myBitmap.GetThumbnailImage(w, 75, myCallback, IntPtr.Zero);
            Bitmap origi = new Bitmap(myThumbnail);
            Bitmap recThumb = new Bitmap(75, 75);

            for (int i = 0; i < 75; i++)
                for (int j = 0; j < 75; j++)
                {
                    recThumb.SetPixel(i, j, origi.GetPixel(left + i, j));
                }

            recThumb.Save("c:\\bb.jpg", System.Drawing.Imaging.ImageFormat.Jpeg);

            //釋放進行縮圖處理的資源
            origi.Dispose();
            recThumb.Dispose();
        }
        else
        {
            int h = (int)((75.0 / (float)myBitmap.Width) * (float)myBitmap.Height);

            if (h > 10000)
                h = 10000;

            int top = (h - 75) / 2;

            myThumbnail = myBitmap.GetThumbnailImage(75, h, myCallback, IntPtr.Zero);
            Bitmap origi = new Bitmap(myThumbnail);
            Bitmap recThumb = new Bitmap(75, 75);

            for (int i = 0; i < 75; i++)
                for (int j = 0; j < 75; j++)
                {
                    recThumb.SetPixel(i, j, origi.GetPixel(i, top + j));
                }

            recThumb.Save("c:\\cc.jpg", System.Drawing.Imaging.ImageFormat.Jpeg);

            //釋放進行縮圖處理的資源
            origi.Dispose();
            recThumb.Dispose();
        }


        //m size

        string user = "";
        string dir = "c:";
        string thumbname = "aa.jpg";

        int target_w = 106;
        int target_h = 83;
        
			//若圖形較小，直接複製M，不製作縮圖
			if(myBitmap.Width<=target_w && myBitmap.Height<=target_h)
			{
				FileInfo fipic=new FileInfo(dir+user+"\\"+thumbname);
				//fipic.CopyTo(dir+user+"\\s-"+thumbname);
				fipic.CopyTo(dir+user+"\\m-"+thumbname);

				myBitmap.Dispose();
				myThumbnail.Dispose();

				return;
			}

			//判斷類型作 m系列縮圖
			FileInfo testpic=new FileInfo(dir+user+"\\"+thumbname);



			//某些情況下，不製作M縮圖
			//圖檔比例超過一定範圍者
			if(((myBitmap.Width/myBitmap.Height)>30)||((myBitmap.Height/myBitmap.Width)>30))
			{
				testpic.CopyTo(dir+user+"\\m-"+thumbname);

				myBitmap.Dispose();
				myThumbnail.Dispose();

				return;
			}

			//20070505 製作縮圖品質提高到90
			if(testpic.Extension.ToLower().Equals(".jpg")||testpic.Extension.ToLower().Equals(".jpeg"))
			{
				//m系列.JPG格式.>50K
				testpic.CopyTo(dir+user+"\\m-"+thumbname);

				if(myBitmap.Width<=500 && myBitmap.Height<=500)
				{
					PS.OnlineImageOptimizer.ImageOptimizer op = new PS.OnlineImageOptimizer.ImageOptimizer(dir+user+"\\m-"+thumbname);
					op.Optimize(90);				
				}
				else
				{
					PS.OnlineImageOptimizer.ImageOptimizer op = new PS.OnlineImageOptimizer.ImageOptimizer(dir+user+"\\m-"+thumbname);
					op.Optimize(500,500,90);				
				}					
			}
			else
			{
				//m系列.非JPG格式
				//如果圖比500小就不作縮圖
				if(myBitmap.Width<=500 && myBitmap.Height<=500)
				{
					myBitmap.Save(dir+user+"\\m-"+thumbname, System.Drawing.Imaging.ImageFormat.Jpeg);				
				}
				else
				{
					if(myBitmap.Width>myBitmap.Height)
					{
						int h=(int)((500.0/(float)myBitmap.Width)*(float)myBitmap.Height);
						myThumbnail = myBitmap.GetThumbnailImage(500, h, myCallback, IntPtr.Zero); 
					}
					else
					{
						int w=(int)((500.0/(float)myBitmap.Height)*(float)myBitmap.Width);
						myThumbnail = myBitmap.GetThumbnailImage(w, 500, myCallback, IntPtr.Zero); 
					}
					myThumbnail.Save(dir+user+"\\"+"m-"+thumbname,System.Drawing.Imaging.ImageFormat.Jpeg);
				}
			}
						
			myBitmap.Dispose();
			myThumbnail.Dispose();
		



    }
}
